package Lesson19Notepad;

import java.util.Scanner;

import javax.script.ScriptException;

public class SecuredNotepad extends SimpleNotepad implements ISecuredNotepad
{
	private  String pass;
	
	
	
	public SecuredNotepad (int NumberOfPages, String password) 
	{
		
		super(NumberOfPages);
		
		if(checkStrongPass(password))
		{
		this.pass = password;
		}
		else
		{
			System.out.println("Invalid password!");
		}
	}

	
	@Override
	public boolean askForPassword() 
	{
		Scanner sc = new Scanner(System.in);
		//System.out.println("Enter a password");
		return sc.next().equals(pass);
	}


	@Override
	public void addText(int page, String text) {
		System.out.println("Enter a password to add text!");
		if(askForPassword())
		super.addText(page, text);
	}


	@Override
	public void addNewText(int page, String newText) {
		System.out.println("Enter a password to add NEW text!");
		if(askForPassword())
		super.addNewText(page, newText);
	}


	@Override
	public void delText(Page selectedPage) {
		System.out.println("Enter a password to DELETE text!");
		if(askForPassword())
		super.delText(selectedPage);
	}


	@Override
	public void printPages() {
		System.out.println("Enter a password to read the pages text!");
		if(askForPassword())
		super.printPages();
	}

	@Override
	public void printAllPagesWithDigits() 
	{
		System.out.println("Enter a password to see all pages with digits!");
		if(askForPassword())
		super.printAllPagesWithDigits();
	}


	private boolean checkStrongPass (String password)
	{
		boolean capitalLetters = false;
		boolean smallLetters = false;
		boolean digits = false;
		if (password.length() >= 5) 
		{
			for (int i = 0; i < password.length(); i++) 
			{
				String str = "" + password.charAt(i);
				if (str.matches("[0-9]")) 
				{
					digits = true;
				}
				if (str.matches("[A-Z]")) 
				{
					capitalLetters = true;
				}
				if (str.matches("[a-z]")) 
				{
					smallLetters = true;
				}
			}
			if (capitalLetters == true && smallLetters == true
					&& digits == true) 
			{
				return true;
			}
		}
		return false;
	}
	


	
	
}

